package cn.hn.yh.controller.ddjh;

import cn.hn.yh.common.DataStory;
import cn.hn.yh.common.utils.DateUtil;
import cn.hn.yh.serviceInterface.ddjh.MyFirstServiceInf;
import cn.hn.yh.vo.response.DataAvgVO;
import cn.hn.yh.vo.response.DataVO;
import cn.hn.yh.vo.response.ReturnVO;
import cn.hn.yh.vo.response.StateVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ddjh/da")
public class ddjhController {
    @Autowired
    private MyFirstServiceInf myFirstServiceInf;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private DateUtil dateUtil;

    @GetMapping("/getSwyd")
    public String getSwyd(String pdate) {
        String str = "";
        List<String[]> list1 = new ArrayList<>();
        List<String[]> list2 = new ArrayList<>();
        List<String[]> list3 = new ArrayList<>();
        List<String[]> data = myFirstServiceInf.getSwyd(pdate);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        if (data.size() > 0) {
            list1.add(data.get(0));
            list2.add(columnname);
            list3.add(data.get(1));
        } else {
            list1.add(null);
            list2.add(columnname);
            list3.add(null);
        }
        Map<String, List> map = new HashMap<>();
        map.put("xAxisList", list2);
        map.put("data1", list1);
        map.put("data2", list3);
        try {
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/getSwyd_table")
    public String getSwyd_table(String pdate) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        List<String[]> data = myFirstServiceInf.getSwyd(pdate);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        if (data.size() > 0) {
            for (int i = 0; i < columnname.length; i++) {
                String[] s = new String[3];
                s[0] = columnname[i] == null ? "0" : columnname[i];
                s[1] = data.get(0)[i] == null ? "0" : data.get(0)[i];
                s[2] = data.get(1)[i] == null ? "0" : data.get(1)[i];
                list.add(s);
            }
        } else {
            for (int i = 0; i < columnname.length; i++) {
                String[] s = new String[3];
                s[0] = columnname[i] == null ? "0" : columnname[i];
                s[1] = "无数据";
                s[2] = "无数据";
                list.add(s);
            }
        }

        Map<String, Object> map = new HashMap<>();
        try {
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", "96");
            map.put("data", list);
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/getSwyd_avg")
    public String getSwyd_avg(String pdate1, String pdate2) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        List<String> dates = dateUtil.getDateList(pdate1, pdate2, "yyyy-MM-dd", "asc");
        for (int i = 0; i < dates.size(); i++) {
            String[] s = new String[3];
            String data = myFirstServiceInf.getSwyd_avg(dates.get(i));
            s[0] = "河南";
            s[1] = dates.get(i);
            s[2] = data;
            list.add(s);
        }
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("code", 0);
            map.put("msg", "");
            map.put("count", "96");
            map.put("data", list);
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping(value = "/vue")
    public String getVueSwyd_table2(String pdate) {
        String str = "";
        List<String[]> list = new ArrayList<>();
        List<String[]> data = myFirstServiceInf.getSwyd(pdate);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        if (data.size() > 0) {
            for (int i = 0; i < columnname.length; i++) {
                String[] s = new String[3];
                s[0] = columnname[i] == null ? "---" : columnname[i];
                s[1] = data.get(0)[i] == null ? "0" : data.get(0)[i];
                s[2] = data.get(1)[i] == null ? "0" : data.get(1)[i];
                list.add(s);
            }
        } else {
            for (int i = 0; i < columnname.length; i++) {
                String[] s = new String[3];
                s[0] = columnname[i] == null ? "0" : columnname[i];
                s[1] = "无数据";
                s[2] = "无数据";
                list.add(s);
            }
        }
        System.out.println(list.get(0)[1]);
        Map<String, Object> mapParent = new HashMap<>();
        Map<String, Object> mapState = new HashMap<>();
        List<ReturnVO> returnList = new ArrayList<>();
        List<Object> dataList = new ArrayList<>();
        try {
            ReturnVO returnVO = new ReturnVO();
            StateVO stateVO = new StateVO();
            stateVO.setCode(200);
            stateVO.setMsg("success");
            returnVO.setState(stateVO);
            for (int i = 0; i < list.size(); i++) {
                DataVO dataVO = new DataVO();
                dataVO.setPoint(list.get(i)[0]);
                dataVO.setReal(list.get(i)[1]);
                dataVO.setPlan(list.get(i)[2]);
                dataList.add(dataVO);
            }
            returnVO.setData(dataList);
            System.out.println(returnVO.getData().get(0).toString());
            str = objectMapper.writeValueAsString(returnVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @PostMapping(value = "/vue_ddjh_table")
    public String getVueSwyd_table(@RequestBody String pdate) {
        System.out.println("pdate===" + pdate);
        String str = "";
        List<String[]> list = new ArrayList<>();
        List<String[]> data = myFirstServiceInf.getSwyd(pdate);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        if (data.size() > 0) {
            for (int i = 0; i < columnname.length; i++) {
                String[] s = new String[3];
                s[0] = columnname[i] == null ? "---" : columnname[i];
                s[1] = data.get(0)[i] == null ? "---" : data.get(0)[i];
                s[2] = data.get(1)[i] == null ? "---" : data.get(1)[i];
                list.add(s);
            }
        } else {
            for (int i = 0; i < columnname.length; i++) {
                String[] s = new String[3];
                s[0] = columnname[i] == null ? "0" : columnname[i];
                s[1] = "无数据";
                s[2] = "无数据";
                list.add(s);
            }
        }
        List<Object> dataList = new ArrayList<>();
        try {
            ReturnVO returnVO = new ReturnVO();
            StateVO stateVO = new StateVO();
            stateVO.setCode(200);
            stateVO.setMsg("success");
            returnVO.setState(stateVO);
            for (int i = 0; i < list.size(); i++) {
                DataVO dataVO = new DataVO();
                dataVO.setPoint(list.get(i)[0]);
                dataVO.setReal(list.get(i)[1]);
                dataVO.setPlan(list.get(i)[2]);
                dataList.add(dataVO);
            }
            returnVO.setData(dataList);
            str = objectMapper.writeValueAsString(returnVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/vue_ddjh_avg")
    public String getVueSwyd_avg(@RequestParam("start") String start, @RequestParam("end") String end) {
        System.out.println("start===" + start);
        System.out.println("end===" + end);
        String str = "";
        List<String[]> list = new ArrayList<>();
        List<String> dates = dateUtil.getDateList(start, end, "yyyy-MM-dd", "asc");
        for (int i = 0; i < dates.size(); i++) {
            String[] s = new String[3];
            String data = myFirstServiceInf.getSwyd_avg(dates.get(i));
            s[0] = "河南";
            s[1] = dates.get(i);
            s[2] = data;
            list.add(s);
        }
        List<Object> dataList = new ArrayList<>();
        try {
            ReturnVO returnVO = new ReturnVO();
            StateVO stateVO = new StateVO();
            stateVO.setCode(200);
            stateVO.setMsg("success");
            returnVO.setState(stateVO);
            for (int i = 0; i < list.size(); i++) {
                DataAvgVO dataAvgVO = new DataAvgVO();
                dataAvgVO.setName(list.get(i)[0]);
                dataAvgVO.setDate(list.get(i)[1]);
                dataAvgVO.setAvg(list.get(i)[2]);
                dataList.add(dataAvgVO);
            }
            returnVO.setData(dataList);
            str = objectMapper.writeValueAsString(returnVO);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }

    @GetMapping("/vue_da_ddjh_chart")
    public String getdaDdjhChart(String pdate) {
        String str = "";
        List<String[]> list1 = new ArrayList<>();
        List<String[]> list2 = new ArrayList<>();
        List<String[]> list3 = new ArrayList<>();
        List<String[]> data = myFirstServiceInf.getSwyd(pdate);
        String[] columnname = DataStory.DAY_LOAD_96COLUMN_NAME;
        if (data.size() > 0) {
            list1.add(data.get(0));
            list2.add(columnname);
            list3.add(data.get(1));
        } else {
            list1.add(null);
            list2.add(columnname);
            list3.add(null);
        }
        Map<String, List> map = new HashMap<>();
        map.put("xAxisList", list2);
        map.put("data1", list1);
        map.put("data2", list3);
        try {
            str = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return str;
    }
}
